const lineNumbers = (rawCode) => {
    const code = rawCode.slice(
        rawCode.indexOf('<code>'),
        rawCode.indexOf('</code>')
    )

    const lines = code.split('\n')
    const lineNumbersCode = [...Array(lines.length - 1)]
        .map((line, index) => `<span class="line-number">${index + 1}</span><br>`).join('')

    const lineNumbersWrapperCode =
        `<div class="line-numbers-wrapper">${lineNumbersCode}</div>`

    const finalCode = rawCode
        .replace('<!--beforeend-->', `${lineNumbersWrapperCode}<!--beforeend-->`)
        .replace('extra-class', 'line-numbers-mode')

    return finalCode
}

module.exports = lineNumbers
